package linter.controller.note;

import cn.xuyanwu.spring.file.storage.MockMultipartFile;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import linter.POJO.DO.Note;
import linter.POJO.DTO.NoteDTO;
import linter.POJO.DTO.UserDTO;
import linter.custom.ResultVO;
import linter.service.NoteCommunityService;
import linter.service.NoteService;
import linter.service.UserService;
import linter.utils.JwtUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.SecurityUtils;
import org.springframework.util.StreamUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.FileInputStream;
import java.io.IOException;

import static linter.custom.CustomResultType.USER_NOT_EXIST;

/**
 * @author 北煜
 */
@Slf4j
@Api(tags = "笔记管理器")
@RestController
@RequestMapping("/note")
public class NoteController {

    @Resource
    NoteService noteService;
    @Resource
    UserService userService;
    @Resource
    NoteCommunityService noteCommunityService;



    @GetMapping("/show/getUserByUsername/{username}")
    @ApiOperation("根据用户名查找用户")
    public ResultVO<UserDTO> getUserByUsername(@PathVariable String username) {
        UserDTO userDTO = userService.getUserByUsername(username);
        return ResultVO.success(userDTO);
    }

    // TODO: 改为保存HTML格式文件
    @PostMapping("/setJSON")
    @ApiOperation("创建笔记--JSON数据")
    public ResultVO<?> setNoteForJson(@RequestBody String noteJson) {
        String username = JwtUtils.getUserName((String) SecurityUtils.getSubject().getPrincipal());
        Long uuid = userService.getIdByUsername(username);
        if (uuid != null){ return ResultVO.success(noteService.setNoteForJson(uuid, noteJson)); }
        return ResultVO.error(USER_NOT_EXIST);
    }

    @PostMapping("/isShareNote")
    @ApiOperation("是否共享笔记")
    public ResultVO<String> isShareNote(@RequestBody int isShare, @RequestBody Long noteId) {
        String username = JwtUtils.getUserName((String) SecurityUtils.getSubject().getPrincipal());
        Long uuid = userService.getIdByUsername(username);
        if (uuid != null){ return ResultVO.success(noteCommunityService.isShareNote(uuid, isShare, noteId)); }
        return ResultVO.error(USER_NOT_EXIST);
    }

    @PostMapping("/getJSONByUuid")
    @ApiOperation("根据uuid获取用户笔记内容")
    public ResultVO<?> getJsonByUuid(@RequestBody Long noteId) {
        String username = JwtUtils.getUserName((String) SecurityUtils.getSubject().getPrincipal());
        Long uuid = userService.getIdByUsername(username);
        if (uuid != null){ return ResultVO.success(noteService.getNoteByUuid(uuid, noteId)); }
        return ResultVO.error(USER_NOT_EXIST);
    }

    @DeleteMapping("/DeleteNoteByNoteId")
    @ApiOperation("根据笔记ID进行删除")
    public ResultVO<String> deleteNoteByNoteId(@RequestParam Long noteId) {
        String username = JwtUtils.getUserName((String) SecurityUtils.getSubject().getPrincipal());
        Long uuid = userService.getIdByUsername(username);
        if (uuid != null){ return ResultVO.success(noteService.deleteNoteByNoteId(noteId, uuid)); }
        return ResultVO.error(USER_NOT_EXIST);
    }

    @PostMapping("/GetAllNoteByUser")
    @ApiOperation("根据用户查询所有的笔记并进行分页 ")
    public ResultVO<PageInfo<NoteDTO>> getAllNoteByUser(@RequestParam(value = "pageNum",defaultValue = "1") int pageNum,
                                                        @RequestParam(value = "pageSize",defaultValue = "8") int pageSize) {
        String username = JwtUtils.getUserName((String) SecurityUtils.getSubject().getPrincipal());
        Long uuid = userService.getIdByUsername(username);
        if (uuid != null){ return ResultVO.success(noteService.getAllNote(pageSize, pageNum, uuid)); }
        return ResultVO.error(USER_NOT_EXIST);
    }

    @PostMapping("/UpdateNoteJsonByUuid")
    @ApiOperation("根据用户ID来修改笔记内容")
    public ResultVO<String> updateNoteJsonByUser(@RequestParam Long noteId, @RequestBody String noteJson) {
        String username = JwtUtils.getUserName((String) SecurityUtils.getSubject().getPrincipal());
        Long uuid = userService.getIdByUsername(username);
        if (uuid != null){
            return ResultVO.success(noteService.updateNoteJsonByUser(uuid, noteId, noteJson));
        }
        return ResultVO.error(USER_NOT_EXIST);
    }

    @PostMapping("/getRecentNote")
    @ApiOperation("获取最近编辑的笔记")
    public ResultVO<PageInfo<Note>> getRecentFiles(@RequestParam(value = "pageNum",defaultValue = "1") int pageNum,
                                                   @RequestParam(value = "pageSize",defaultValue = "10") int pageSize){
        String username = JwtUtils.getUserName((String) SecurityUtils.getSubject().getPrincipal());
        Long uuid = userService.getIdByUsername(username);
        if (uuid != null){ return ResultVO.success(noteService.getRecentFiles(uuid, pageSize, pageNum)); }
        return ResultVO.error(USER_NOT_EXIST);
    }

    @PostMapping("/createNoteNull")
    @ApiOperation("创建一个空的笔记")
    public ResultVO<NoteDTO> createNoteNull(@RequestParam("file") MultipartFile file, String noteName) throws IOException {
        String username = JwtUtils.getUserName((String) SecurityUtils.getSubject().getPrincipal());
        Long uuid = userService.getIdByUsername(username);
        if (uuid != null) {
            if (file.isEmpty()) {
                // 读取默认图片文件内容
                FileInputStream inputStream = new FileInputStream("src/main/resources/static/defaultImg/daf_note_create_img.png");
                byte[] bytes = StreamUtils.copyToByteArray(inputStream);
                // 关闭流
                inputStream.close();
                // 使用MockMultipartFile创建模拟的MultipartFile
                file = new MockMultipartFile("defaultImage",
                        "daf_note_create_img.png", "image/png", bytes);
            }
            return ResultVO.success(noteService.createNoteNull(uuid, file, noteName));
        }
        return ResultVO.error(USER_NOT_EXIST);
    }


    @PostMapping("/getViewsNote")
    @ApiOperation("获取最近浏览量过的笔记")
    public ResultVO<PageInfo<NoteDTO>> getViewsFiles(@RequestParam(value = "pageNum",defaultValue = "1") int pageNum,
                                         @RequestParam(value = "pageSize",defaultValue = "5") int pageSize){
        String username = JwtUtils.getUserName((String) SecurityUtils.getSubject().getPrincipal());
        Long uuid = userService.getIdByUsername(username);
        if (uuid != null){ return ResultVO.success(noteService.getViewsNote(uuid, pageSize, pageNum)); }
        return ResultVO.error(USER_NOT_EXIST);
    }

    @PostMapping("/selNotCollectNote")
    @ApiOperation("获取未收藏的笔记")
    public ResultVO<PageInfo<NoteDTO>> selNotCollectNote(){
        String username = JwtUtils.getUserName((String) SecurityUtils.getSubject().getPrincipal());
        Long uuid = userService.getIdByUsername(username);
        if (uuid != null){ return ResultVO.success(noteService.selNotCollectNote(uuid)); }
        return ResultVO.error(USER_NOT_EXIST);
    }

    @PostMapping("/getNoteClassByUuid")
    @ApiOperation("查询课程笔记根据用户ID")
    public ResultVO<PageInfo<NoteDTO>> getNoteClassByUuid() {
        String username = JwtUtils.getUserName((String) SecurityUtils.getSubject().getPrincipal());
        Long uuid = userService.getIdByUsername(username);
        if (uuid != null) {
            return ResultVO.success(noteService.getNoteClassByUuid(uuid));
        }
        return ResultVO.error(USER_NOT_EXIST);
    }

    @PostMapping("/selNoteClass")
    @ApiOperation("查询课程笔记")
    public ResultVO<PageInfo<NoteDTO>> selNoteClass(String noteTitle) {
        String username = JwtUtils.getUserName((String) SecurityUtils.getSubject().getPrincipal());
        Long uuid = userService.getIdByUsername(username);
        if (uuid != null) {
            return ResultVO.success(noteService.selNoteClass(uuid,noteTitle));
        }
        return ResultVO.error(USER_NOT_EXIST);
    }

}
